let formidable = require('formidable');
let path = require('path');
// 引入表结构
let Product = require('../../db/productSchame');
let form = formidable({
    keepExtensions: true,
    uploadDir: path.join(__dirname, '../', '../', 'public', 'uploads')
});

module.exports = (req, res) => {
    console.log('进来了');
    form.parse(req, async(err, fields, files) => {
        // 除了file文件里的图片内容,其余的内容都可以在fields
        console.log(fields);
        // file文件上传的内容
        console.log(files);
        if (err) {
            res.status(500).send(err);
        } else {
            let addObj = {
                title: fields.title,
                // 一级
                cate_id: fields.cate_id,
                // 二级分类
                goods_id: fields.goods_id,
                pic: files.pic.filepath.split('public')[1],
                price: fields.price,
                fee: fields.fee,
                description: fields.description
            };
            console.log(addObj);
            try {
                await Product.create(addObj);
                console.log('创建表成功');
                res.redirect('/product/productlist');
            } catch (error) {
                console.log(error);
            }

        }
    })

}